var app = getApp()
var wxParse = require('../../templates/wxParse/wxParse.js')
var auth = require('../../templates/auth/auth.js')
var getAppGlobalData = require('../../templates/copyright/copyright.js');
var timer;
var timerUnread;
var bbb_interval;
Page({
  data: {
    // 通用 
    globalData: {},
    clientUnread: '',
    loading: false,
    show: false,
    page: 1,
    more: true,
    isEmpty: false,
    authStatus: 400,
    showTabBar: false,
    currentTabBar: 'cardList',
    toLeavingMessage: '',
    //员工信息
    avatarUrl: '',
    avatarName: '',
    // 名片列表
    customID: '',
    company: [],
    collectionList: [],
    // 名片
    to_uid: 0,
    from_id: 0,
    moreStatus: 1,
    playPushStatus: 1,
    showShareStatus: 0,
    cardZanType: '',
    cardIndexData: [],
    extensionsSelfList: [],
    // 商城
    shopList: [],
    pageShop: 1,
    moreShop: true,
    isEmptyShop: false,
    // 动态
    newsList: [],
    pageNews: 1,
    moreNews: true,
    isEmptyNews: false,
    evaStatus: false,
    newsCurIndex: '-1',
    currentNewsIndex: '',
    evaContent: '',
    ThumbsId: '',
    evaId: '',
    // 公司官网
    swiperStatus: {
      indicatorDots: false,
      autoplay: true
    },
    swiperIndexCur: 0,
    companyData: [],
    tmpContents: [],
  },
  ddd: function () {
    var that = this;
    if (app.globalData.to_uid == 0) {
      wx.showModal({
        title: '',
        content: '不能与默认客服进行对话哦！',
        confirmText: '知道啦',
        showCancel: false,
        success: res => {
          if (res.confirm) {
          } else {
          }
        }
      });
    } else if (app.globalData.to_uid == wx.getStorageSync("userid")) {
      wx.showModal({
        title: '',
        content: '不能和自己进行对话哦！',
        confirmText: '知道啦',
        showCancel: false,
        success: res => {
          if (res.confirm) {
          } else {
          }
        }
      });
    } else {
      wx.navigateTo({
        url: '/longbing_card/chat/userChat/userChat?chat_to_uid=' + app.globalData.to_uid + '&contactUserName=' + app.globalData.nickName
      })
    }
  },
  boss:function(){
    wx.redirectTo({
      url:'../bosss/index'
    })
  },
  bbb: function () {
    let that = this
    app.util.request({
      'url': 'entry/wxapp/ClientUnread',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': {
        // page: 1
        to_uid: app.globalData.to_uid
      },
      success: function (res) {
        console.log('bbb')
        if (that.data.clientUnread < res.data.data.count) {
          that.setData({
            img: that.data.cardIndexData.info.avatar,
          })
          console.log(that.img)
          setTimeout(function () {
            // console.log('ccc')
            that.setData({
              img: '',
            })
          }, 2000)
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  onLoad: function (options) {
    var that = this;
    getAppGlobalData.getAppGlobalData(that);
    that.setData({
      globalData: getApp().globalData
    })


    console.log(app.globalData, "***************//app.globalData")
    that.getClientPhone();

    // 页面初始化 options为页面跳转所带来的参数
    console.log("onLoad", options);

    that.setData({
      bbbid: that.data.to_uid
    })
    /*来自分_享*/
    console.log("options************", options)

    if (options.currentTabBar) {

      that.setData({
        showTabBar: true,
        currentTabBar: options.currentTabBar
      })

      if (options.currentTabBar == 'toCard') {
        wx.setNavigationBarTitle({
          title: '名片',
        })
      } else if (options.currentTabBar == 'toShop') {
        wx.setNavigationBarTitle({
          title: '商城',
        })
      } else if (options.currentTabBar == 'toNews') {
        wx.setNavigationBarTitle({
          title: '动态'
        })
      } else if (options.currentTabBar == 'toCompany') {
        wx.setNavigationBarTitle({
          title: '官网'
        })
      }
    } else {
      that.setData({
        showTabBar: false,
        currentTabBar: 'cardList'
      })
    }

    if (options.to_uid) {
      that.setData({
        to_uid: options.to_uid
      })
      app.globalData.to_uid = that.data.to_uid;
    }

    if (options.from_id) {
      that.setData({
        from_id: options.from_id
      })
      app.globalData.from_id = that.data.from_id;
    }

    if (options.custom) {
      that.setData({
        customID: options.custom
      })
      that.getCustomQrRecordInsert();
    }

    //// console.log("来自分_享",options.to_uid,options.from_id,options.currentTabBar,that.data.to_uid,that.data.from_id)
    //// console.log("重置  app.globalData.to_uid || from_id", app.globalData.to_uid, app.globalData.from_id);

    if (wx.getStorageSync("formID")) {
      let formID = wx.getStorageSync("formID");
      formID = that.data.from_id;
      wx.setStorageSync("formID", formID)
    } else {
      wx.setStorageSync("formID", that.data.from_id)
    }

    if (that.data.currentTabBar == 'cardList') {
      //// console.log("名片列表 =========================onShow==")
      wx.hideShareMenu();
      that.setData({
        collectionList: [],
        show: false,
        authStatus: 400,
        showTabBar: false,
      })
      that.getCollectionList();
    } else if (that.data.currentTabBar == 'toCard') {
      // console.log("名片 =========================onShow==")
      that.setData({
        cardIndexData: [],
        extensionsSelfList: [],
        show: false,
        authStatus: 400,
      })
      that.getCardIndexData();
      if (that.data.to_uid == wx.getStorageSync("userid")) {
        that.ExtensionsSelf();
      }

      timer = setInterval(function () {
        console.log(app.globalData.encryptedData, "1111111111111111111111  encryptedData");
        if (app.globalData.encryptedData) {
          that.toGetShareInfo();
          clearInterval(timer);
        }
      }, 1000)


    } else if (that.data.currentTabBar == 'toShop') {
      // console.log("商城 =========================onShow==")
      that.setData({
        shopList: [],
        pageShop: 1,
        moreShop: true,
        loading: false,
        isEmptyShop: false,
        show: false,
        authStatus: 400,
      })
      that.getShopList();
    } else if (that.data.currentTabBar == 'toNews') {
      // console.log("动态 =========================onShow==")
      that.setData({
        newsList: [],
        pageNews: 1,
        moreNews: true,
        loading: false,
        isEmptyNews: false,
        show: false,
        authStatus: 400,
      })
      that.getNewsList();
    } else if (that.data.currentTabBar == 'toCompany') {
      // console.log("公司官网 =========================onShow==")
      that.setData({
        companyData: [],
        show: false,
        authStatus: 400,
      })
      that.getCompanyData();
    }



  },
  onReady: function () {
    // console.log("页面渲染完成")
  },
  onShow: function () {
    // 页面显示
    var that = this;
    clearInterval(timerUnread);

    if (that.data.currentTabBar != 'cardList') {
      if (that.data.to_uid != wx.getStorageSync("userid")) {
        that.getClientUnread();
      }
    }

    if (that.data.currentTabBar == 'cardList') {
      that.setData({
        showTabBar: false
      })
    } else {
      that.setData({
        showTabBar: true
      })
    }
    that.checkAuthStatus();
  },
  onHide: function () {
    // console.log("页面隐藏")
    clearInterval(timer);
    clearInterval(timerUnread);
    clearInterval(bbb_interval);
    console.log("onHide ==> timer || timerUnread || bbb_interval")
  },
  onUnload: function () {
    // console.log("页面关闭")
    clearInterval(timer);
    clearInterval(timerUnread);
    clearInterval(bbb_interval);
    console.log("onUnload ==> timer || timerUnread || bbb_interval")
  },
  onPullDownRefresh: function () {
    console.log(`下啦：${this.data.showTabBar}`)
    // console.log("监听用户下拉动作")
    var that = this;
    that.setData({
      loading: false,
      showTabBar: false,
    })
    if (that.data.currentTabBar == 'cardList') {
      // console.log("名片列表 =========================onPullDownRefresh==")
      that.setData({
        showTabBar: false,
        onPullDownRefreshCardList: true,
      })
      that.getCollectionList();
    } else if (that.data.currentTabBar == 'toCard') {
      // console.log("名片 =========================onPullDownRefresh==")
      that.setData({
        showTabBar: true
      })
      that.getCardIndexData();
      that.ExtensionsSelf();
    } else if (that.data.currentTabBar == 'toShop') {
      // console.log("商城 =========================onPullDownRefresh==")
      that.setData({
        onPullDownRefreshShop: true,
        pageShop: 1,
        moreShop: true,
        isEmptyShop: false,
        showTabBar: true
      })
      that.getShopList();
    } else if (that.data.currentTabBar == 'toNews') {
      // console.log("动态 =========================onPullDownRefresh==")
      that.setData({
        onPullDownRefreshNews: true,
        pageNews: 1,
        moreNews: true,
        isEmptyNews: false,
        showTabBar: true
      })
      that.getNewsList();
    } else if (that.data.currentTabBar == 'toCompany') {
      // console.log("公司官网 =========================onPullDownRefresh==")
      that.setData({
        showTabBar: true
      })
      that.getCompanyData();
    }
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 3000);
  },
  onReachBottom: function () {
    // console.log("监听页面上拉触底")
    var that = this;
    that.setData({
      show: true,
      showTabBar: false
    })
    if (that.data.currentTabBar == 'cardList') {
      // console.log("名片列表 =========================onReachBottom==")
      if (that.data.isEmpty == false) {
        that.setData({
          page: that.data.page + 1
        })
      that.getCollectionList();
      }
      that.setData({
        showTabBar: false,
      })
    } else if (that.data.currentTabBar == 'toCard') {
      // console.log("名片 =========================onReachBottom==")
    } else if (that.data.currentTabBar == 'toShop') {
      // console.log("商城 =========================onReachBottom==")
      that.setData({
        showTabBar: true
      })
      if (that.data.isEmptyShop == false) {
        that.setData({
          pageShop: that.data.pageShop + 1
        })
        that.getShopList();
      }
    } else if (that.data.currentTabBar == 'toNews') {
      // console.log("动态 =========================onReachBottom==")
      that.setData({
        showTabBar: true
      })
      if (that.data.isEmptyNews == false) {
        that.setData({
          pageNews: that.data.pageNews + 1
        })
        that.getNewsList();
      }
    } else if (that.data.currentTabBar == 'toCompany') {
      // console.log("公司官网 =========================onReachBottom==")
      that.setData({
        showTabBar: true
      })
    }
  },
  onPageScroll: function (e) {
    // console.log("监听页面滚动", e);
    var that = this;
    that.setData({
      evaStatus: false,
      newsCurIndex: '-1',
    })
    if (that.data.currentTabBar != 'cardList') {
      that.setData({
        showTabBar: true
      })
    }
  },
  onShareAppMessage: function (res) {
    // console.log("用户点击右上角分_享")
    var that = this;
    if (that.data.currentTabBar == 'toCard') {
      var tmpData = that.data.cardIndexData;
      if (res.from === 'button') {
        // console.log("来自页面内转发按钮");
      }
      else {
        // console.log("来自右上角转发菜单");
      } 

      return {
        title: '您好，我是' + app.globalData.company.name +'的' +tmpData.info.job_name +  tmpData.info.name +'，请惠存！',
        path: '/longbing_card/pages/index/index?to_uid=' + app.globalData.to_uid + '&from_id=' + wx.getStorageSync("userid") + '&currentTabBar=toCard',
        imageUrl: tmpData.info.avatar,
        success: function (res) {
          console.log("转发成功", res)
          that.toShareRecord();
          if (that.data.to_uid != wx.getStorageSync("userid")) {
            that.toForwardRecord(1, 0);
          }
        },
        fail: function (res) {
          console.log('转发失败');
        }
      };
    } else if (that.data.currentTabBar == 'toShop') {
      if (res.from === 'button') {
        // console.log("来自页面内转发按钮");
      }
      else {
        // console.log("来自右上角转发菜单")
      }
      return {
        title: '',
        path: '/longbing_card/pages/index/index?to_uid=' + app.globalData.to_uid + '&from_id=' + wx.getStorageSync("userid") + '&currentTabBar=toShop',
        imageUrl: '',
        success: function (res) {
          console.log("转发成功", res)
        },
        fail: function (res) {
          console.log('转发失败');
        }
      };
    } else if (that.data.currentTabBar == 'toNews') {
      // console.log(res, "****//////////////////***************")
      if (res.from === 'button') {
        var index = res.target.dataset.index;
        var id = res.target.dataset.id;
        var tmpData = that.data.newsList;
        return {
          title: tmpData[index].title,
          path: '/longbing_card/pages/news/detail/detail?to_uid=' + app.globalData.to_uid + '&from_id=' + wx.getStorageSync("userid") + '&id=' + id,
          imageUrl: tmpData[index].cover,
          success: function (res) {
            console.log("转发成功", res)
            if (that.data.to_uid != wx.getStorageSync("userid")) {
              that.toForwardRecord(3, id);
            }
          },
          fail: function (res) {
            console.log('转发失败');
          }
        };
      }
      else {
        return {
          title: '',
          path: '/longbing_card/pages/index/index?to_uid=' + app.globalData.to_uid + '&from_id=' + wx.getStorageSync("userid") + '&currentTabBar=toNews',
          imageUrl: ''
        };
      }
    } else if (that.data.currentTabBar == 'toCompany') {
      if (res.from === 'button') {
        // console.log("来自页面内转发按钮");
      }
      else {
        // console.log("来自右上角转发菜单")
      }
      return {
        title: '',
        path: '/longbing_card/pages/index/index?to_uid=' + app.globalData.to_uid + '&from_id=' + wx.getStorageSync("userid") + '&currentTabBar=toCompany',
        imageUrl: '',
        success: function (res) {
          console.log('转发成功');
          if (that.data.to_uid != wx.getStorageSync("userid")) {
            that.toForwardRecord(4, 0);
          }
        },
        fail: function (res) {
          console.log('转发失败');
        }
      };
    }
  },
  getClientPhone: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/UserPhone',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': {
        client_id: wx.getStorageSync("userid")
      },
      success: function (res) {
        // console.log("entry/wxapp/UserPhone ==>", res)
        if (!res.data.errno) {
          if (res.data.data.phone) {
            app.globalData.hasClientPhone = true
            that.setData({
              globalData: app.globalData
            })
          }
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  getClientUnread: function () {



    let that = this

    that.bbb()

    app.util.request({
      'url': 'entry/wxapp/ClientUnread',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': {
        // page: 1
      },
      success: function (res) {
        // console.log(res)
        that.setData({
          to_uid: app.globalData.to_uid,
          clientUnread: res.data.data.count
        })
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  getCustomQrRecordInsert: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/customQrRecordInsert',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        to_uid: that.data.to_uid,
        qr_id: that.data.customID
      },
      success: function (res) {
        // console.log("entry/wxapp/customQrRecordInsert ==>", res)
        if (!res.data.errno) {
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  // 名片列表
  getCollectionList: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/cards',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        page: 1
        // page: that.data.page
      },
      success: function (res) {
        // console.log(res)
        if (!res.data.errno) {

          var tmpData = res.data.data.list;
          if (tmpData.length == 0) {
            that.setData({
              more: false,
              loading: false,
              isEmpty: true
            })
            return false;
          }
          that.setData({
            loading: true
          })
          if(that.data.onPullDownRefreshCardList == true){
            that.setData({
              collectionList : []
            })
          }
          var tmpCollData = that.data.collectionList;
          for (let i in tmpData) {
            tmpData[i].create_time += '';
            console.log(typeof(tmpData[i].create_time),"*********////")
            var date = new app.util.date();
            if (tmpData[i].create_time.length < 12) {
              tmpData[i].create_time = date.dateToStr('yyyy年MM月DD日 HH:mm:ss  ', date.longToDate(tmpData[i].create_time * 1000));
            }
            tmpCollData.push(tmpData[i]);
          }
          that.setData({
            collectionList: tmpCollData
            // collectionList: tmpData
          })
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  // 名片
  getCardIndexData: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/card',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        to_uid: that.data.to_uid,
        from_id: that.data.from_id
      },
      success: function (res) {
        console.log("entry/wxapp/index ==>", that.data.to_uid, that.data.from_id)
        console.log("entry/wxapp/index ==>", res)
        if (!res.data.errno) {

          let tmpDataIndex = res.data.data;

          for (let i in tmpDataIndex.info.images) {
            if (!tmpDataIndex.info.images[i]) {
              tmpDataIndex.info.images.splice(i, 1)
            }
          }

          that.setData({
            cardIndexData: tmpDataIndex,
            to_uid: tmpDataIndex.to_uid,
            from_id: tmpDataIndex.from_id,
          })
          app.globalData.to_uid = tmpDataIndex.to_uid;
          app.globalData.from_id = tmpDataIndex.from_id;
          if (wx.getStorageSync("userid") == that.data.cardIndexData.to_uid) {
            app.globalData.isStaff = true;
            that.setData({
              globalData: app.globalData
            })
          }

          console.log(app.globalData.isStaff, "app.globalData.isStaff")
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  ExtensionsSelf: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/ExtensionsSelf',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        to_uid: app.globalData.to_uid
      },
      success: function (res) {
        // console.log("entry/wxapp/index ==>", res)
        if (!res.data.errno) {
          that.setData({
            extensionsSelfList: res.data.data
          })
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  toEditPraiseStatus: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/thumbs',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        to_uid: app.globalData.to_uid,
        type: that.data.cardZanType
      },
      success: function (res) {
        // console.log("entry/wxapp/index ==>", res)
        if (!res.data.errno) {
          var tmpData = that.data.cardIndexData;
          if (that.data.cardZanType == 3) {
            if (tmpData.isThumbs == 1) {
              tmpData.thumbs_up = tmpData.thumbs_up * 1 - 1;
              tmpData.isThumbs = 0;
            } else if (tmpData.isThumbs == 0) {
              tmpData.isThumbs = 1;
              tmpData.thumbs_up = tmpData.thumbs_up * 1 + 1;
            }
            // console.log(tmpData.isThumbs, "isThumbs")
          } else if (that.data.cardZanType == 1) {
            if (tmpData.voiceThumbs == 1) {
              tmpData.voiceThumbs = 0;
            } else if (tmpData.voiceThumbs == 0) {
              tmpData.voiceThumbs = 1;
            }
            // console.log(tmpData.voiceThumbs, "voiceThumbs")
          }
          that.setData({
            cardIndexData: tmpData
          })
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  toGetShareInfo: function () {
    var that = this;
    // type 1=>浏览名片 2=>浏览自定义码 3=>浏览商品 4=>浏览动态
    // 当type 为2,3,4时, 需要传浏览对象的id 
    wx.login({
      success: function (res) {
        console.log('wx.login ==>>', res);
        let tmpData = {
          encryptedData: app.globalData.encryptedData,
          iv: app.globalData.iv,
          type: 1,
          code: res.code,
          to_uid: that.data.to_uid
        }
        app.util.request({
          'url': 'entry/wxapp/getShare',
          'cachetime': '30',
          'showLoading': false,
          'method': 'POST',
          'data': tmpData,
          success: function (res) {
            console.log("entry/wxapp/getShare ==>", res)
            if (!res.data.errno) {
              console.log(res.data.data)
            }
          },
          fail: function (res) {
            console.log("fail ==> ", res)
          }
        })
      }
    })
  },
  // 商城
  getShopList: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/goods',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        page: that.data.pageShop,
        to_uid: app.globalData.to_uid
      },
      success: function (res) {
        // console.log("entry/wxapp/goods ==>", res)
        if (!res.data.errno) {
          var tmpData = res.data.data.list;
          if (tmpData.length == 0) {
            that.setData({
              moreShop: false,
              loading: false,
              isEmptyShop: true,
              show: true
            })
            return false;
          }
          that.setData({
            loading: true
          })
          if (that.data.onPullDownRefreshShop == true) {
            that.setData({
              shopList: []
            })
          }
          var tmpShopData = that.data.shopList;
          for (let i in tmpData) {
            tmpShopData.push(tmpData[i]);
          }
          that.setData({
            shopList: tmpShopData,
            onPullDownRefreshShop: false
          })
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  // 动态
  getNewsList: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/timeline',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        page: that.data.pageNews,
        to_uid: app.globalData.to_uid
      },
      success: function (res) {
        if (!res.data.errno) {

          var tmpData = res.data.data.list;
          if (tmpData.length == 0) {
            that.setData({
              moreNews: false,
              loading: false,
              isEmptyNews: true,
              show: true
            })
            return false;
          }
          that.setData({
            loading: true
          })
          if (that.data.onPullDownRefreshNews == true) {
            that.setData({
              newsList: []
            })
          }
          var tmpNewsData = that.data.newsList;
          for (let i in tmpData) {
            var date = new app.util.date();
            if (tmpData[i].create_time.length < 12) {
              tmpData[i].create_time = date.dateToStr('yyyy.MM.DD', date.longToDate(tmpData[i].create_time * 1000));
            }
            tmpNewsData.push(tmpData[i]);


            if (tmpData[i].cover.indexOf(",") != -1) {
              var tmpArr = tmpData[i].cover.split(',');
              tmpData[i].cover = tmpArr[0];
            }

            console.log(tmpData[i], "****************************//cover")
          }
          that.setData({
            newsList: tmpNewsData,
            onPullDownRefreshNews: false
          })
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  addEva: function (e) {
    var that = this;
    var content = e.detail.value;
    that.setData({
      evaContent: content
    })
  },
  addEvaBtn: function () {
    var that = this;
    if (that.data.evaContent == '') {
      return false;
    }
    that.getComment();
  },
  getThumbs: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/timelineThumbs',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': {
        id: that.data.ThumbsId,
        to_uid: app.globalData.to_uid
      },
      success: function (res) {
        if (!res.data.errno) {
          that.setData({
            evaStatus: false,
            showTabBar: true,
          })
          that.getNewThumbsComment(that.data.ThumbsId);
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  getComment: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/timelineComment',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': {
        id: that.data.evaId,
        to_uid: app.globalData.to_uid,
        content: that.data.evaContent
      },
      success: function (res) {
        if (!res.data.errno) {
          that.setData({
            evaStatus: false,
            showTabBar: true,
          })
          that.getNewThumbsComment(that.data.evaId);
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  getNewThumbsComment: function (id) {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/timelineNew',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': {
        id: id,
        to_uid: app.globalData.to_uid
      },
      success: function (res) {
        if (!res.data.errno) {
          var tmpData = that.data.newsList;
          var index = that.data.currentNewsIndex;
          tmpData[index].thumbs = res.data.data.thumbs;
          tmpData[index].comments = res.data.data.comments;
          that.setData({
            newsList: tmpData,
            evaStatus: false,
            showTabBar: true,
            evaContent: '',
            ThumbsId: '',
            evaId: '',
            index: ''
          })
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  // 官网
  getCompanyData: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/modular',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        to_uid: app.globalData.to_uid
      },
      success: function (res) {
        // console.log("entry/wxapp/modular ==>", res)
        if (!res.data.errno) {

          that.setData({
            markers: [{
              iconPath: "/longbing_card/resource/images/map.png",
              id: 1,
              callout: {
                content: that.data.globalData.company.name,
                fontSize: 14,
                bgColor: '#ffffff',
                padding: 4,
                display: 'ALWAYS',
                textAlign: 'center',
                borderRadius: 2,
              },
              latitude: that.data.globalData.company.latitude,
              longitude: that.data.globalData.company.longitude,
              width: 30,
              height: 30
            }]
          })

          let tmpData = res.data.data;

          let tmpContents = [];
          let tmpContents2 = [];
          for (let i in tmpData) {
            if (tmpData[i].type == 2) {
              wxParse.wxParse(tmpData[i].identification, 'html', tmpData[i].info.content, that, 5);
              tmpContents.push(tmpData[i].identification)
            } else if (tmpData[i].type == 1 || tmpData[i].type == 3 || tmpData[i].type == 5) {
              for (let j in tmpData[i].list) {
                var date = new app.util.date();
                tmpData[i].list[j].create_time = date.dateToStr('yyyy-MM-DD', date.longToDate(tmpData[i].list[j].create_time * 1000));
              }
            }
          }
          for (var i of tmpContents) {
            tmpContents2.push(that.data[i])
          }
          // console.log('tmpContents2', tmpContents2);
          that.setData({
            companyData: tmpData,
            tmpContents: tmpContents2
          })
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  swiperChange: function (e) {
    let that = this;
    let cur = e.detail.current;
    console.log(e, "*******/swiperChange")
    that.setData({
      swiperIndexCur: cur
    });
  },
  // 1=>转发名片 2=>转发商品 3=>转发动态 4=>转发公司官网
  // target_id 转发内容的id 当type=2,3时有效
  toForwardRecord: function (type, targetid) {
    var that = this;
    let paramObj = {
      type: type,
      to_uid: app.globalData.to_uid
    }
    if (type == 2 || type == 3) {
      paramObj.target_id = targetid
    }
    console.log("entry/wxapp/Forward ==> paramObj", paramObj)

    app.util.request({
      'url': 'entry/wxapp/Forward',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': paramObj,
      success: function (res) {
        // console.log("entry/wxapp/Forward ==>", res)
        if (!res.data.errno) {
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  // 记录复制文本 拨打电话 查看地址 分_享等情况
  toCopyRecord: function (type) {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/copyRecord',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': {
        type: type,
        to_uid: app.globalData.to_uid
      },
      success: function (res) {
        // console.log("entry/wxapp/copyRecord ==>", res)
        if (!res.data.errno) {
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  toShareRecord: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/record',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': {
        to_uid: app.globalData.to_uid
      },
      success: function (res) {
        // console.log("entry/wxapp/record ==>", res)
        if (!res.data.errno) {
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  getPhoneNumber: function (e) {
    var that = this;
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      console.log("同意授权获取电话号码")
      var encryptedData = e.detail.encryptedData;
      var iv = e.detail.iv;
      console.log(encryptedData, iv)
      that.setPhoneInfo(encryptedData, iv);
    } else if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      console.log("拒绝授权获取电话号码")
    }
    that.ddd();
  },
  setPhoneInfo: function (encryptedData, iv) {
    var that = this;
    console.log(app.globalData.to_uid)
    wx.login({
      success: function (res) {
        console.log('wx.login ==>>', res);
        let tmpData = {
          encryptedData: encryptedData,
          iv: iv,
          code: res.code,
          to_uid: app.globalData.to_uid
        }
        app.util.request({
          'url': 'entry/wxapp/phone',
          'cachetime': '30',
          'method': 'POST',
          'showLoading': false,
          'data': tmpData,
          success: function (res) {
            // console.log("entry/wxapp/update ==>", res)
            if (!res.data.errno) {
              app.globalData.hasClientPhone = true
              that.setData({
                globalData: app.globalData
              })
            }
          }
        })
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  // 检查授权
  checkAuthStatus: function () {
    var that = this;
    auth.checkAuth(that);
  },
  getUserInfo: function (e) {
    var that = this;
    console.log('获取微信用户信息')
    auth.getUserInfo(e);
  },
  // 点击事件
  toJump: function (e) {
    var that = this;

    var status = e.currentTarget.dataset.status;
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var content = e.currentTarget.dataset.content;
    var type = e.currentTarget.dataset.type;
    // console.log("名片列表===========================================")
    if (status == 'toCardIndex') {
      // console.log("名片详情")
      app.globalData.to_uid = that.data.collectionList[index].userInfo.fans_id;
      app.globalData.nickName = that.data.collectionList[index].userInfo.name;
      that.setData({
        to_uid: that.data.collectionList[index].userInfo.fans_id
      })

      wx.showShareMenu();
      that.setData({
        currentTabBar: 'toCard',
        showTabBar: true,
        globalData: app.globalData,
        page: 1,
        more: true,
        loading: false,
        isEmpty: false,
        show: false,
        authStatus: 400,
      })
      wx.setNavigationBarTitle({
        title: app.globalData.config.mini_app_name,
      })
      if (that.data.currentTabBar != 'cardList') {
        if (that.data.to_uid != wx.getStorageSync("userid")) {
          timerUnread = setInterval(function () {
            that.getClientUnread();
          }, 3000)
        }
      }


      // if (that.data.cardIndexData == '') {
        that.getCardIndexData();
      // }



      if (that.data.to_uid == wx.getStorageSync("userid")) {
        if (that.data.extensionsSelfList.length == 0) {
          that.ExtensionsSelf();
        }
      }
      wx.pageScrollTo({
        duration: 0,
        scrollTop: 0,
      });
    }
    // console.log("名片===========================================")
    if (status == 'toCardZan') {
      // console.log("1语音 || 3靠谱")
      that.setData({
        toLeavingMessage: false,
        cardZanType: type
      })

      that.toEditPraiseStatus();

    } else if (status == 'toVoice') {
      const innerAudioContext = wx.createInnerAudioContext();
      if (type == 1) {

        innerAudioContext.autoplay = true;
        innerAudioContext.src = that.data.cardIndexData.info.voice;
        innerAudioContext.onPlay(() => {
          console.log('开始播放')
        })


        that.setData({
          playPushStatus: 2
        });
        if (app.globalData.to_uid != wx.getStorageSync("userid")) {
          that.toCopyRecord(9);
        }
      }
      if (type == 2) {
        // console.log("语音播放 pauseBackgroundAudio")
        innerAudioContext.pause(() => {
          console.log('暂停播放')
        })
        that.setData({
          playPushStatus: 1
        });
      }
      innerAudioContext.onEnded(() => {
        console.log('音频自然播放结束事件')
        that.setData({
          playPushStatus: 1
        });
      })
    } else if (status == 'toCardList') {
      // console.log("名片列表")  
      that.setData({
        showTabBar: false,
        currentTabBar: 'cardList',
        page: 1,
        more: true,
        loading: false,
        isEmpty: false,
        show: false,
        authStatus: 400,
      })
      wx.setNavigationBarTitle({
        title: '名片列表'
      })
      wx.hideShareMenu();
      // if (that.data.collectionList.length == 0) {
        that.getCollectionList();
      // }
      wx.pageScrollTo({
        duration: 0,
        scrollTop: 0,
      });
      clearInterval(timerUnread);

    } else if (status == 'toStaff') {
      console.log("员工端入口")
      wx.switchTab({
        url: "/longbing_card/staff/radar/radar"
      })
    } else if (status == 'toConsult') {
      console.log("聊天")
      that.setData({
        toLeavingMessage: true
      })
      if (app.globalData.to_uid != wx.getStorageSync("userid")) {
        that.toCopyRecord(8);
      }
      if (that.data.globalData.hasClientPhone == true) {
        console.log("用户已授权手机号码")
      }
      that.ddd();
    } else if (status == 'toShareCard') {
      console.log("关闭弹出层")
      if (type == 3) {
        that.setData({
          showShareStatus: 0
        })
      }
    }


    wx.onBackgroundAudioStop(function () {
      that.setData({
        playPushStatus: 1
      });
    })

    // console.log("商城 ===========================================")
    if (status == 'toShopDetail') {
      // console.log("产品详情")
      let tmpData = '';
      if (that.data.currentTabBar == 'toCard') {
        tmpData = that.data.extensionsSelfList;
      } else if (that.data.currentTabBar == 'toShop') {
        tmpData = that.data.shopList;
      }
      wx.navigateTo({
        url: '/longbing_card/pages/shop/detail/detail?id=' + tmpData[index].id
      })
    }

    // console.log("动态 ===========================================")
    if (status == 'toNewsShow') {
      // console.log("显示评论面板")
      that.setData({
        newsCurIndex: index,
        currentNewsIndex: index
      })
    } else if (status == 'toNewsZan') {
      // console.log("点赞")
      console.log(index, "toNewsZan")
      that.setData({
        newsCurIndex: '-1',
        toLeavingMessage: false,
        ThumbsId: id
      })
      that.getThumbs();
    } else if (status == 'toEva') {
      console.log("评论", that.data.evaStatus)
      that.setData({
        newsCurIndex: '-1',
        toLeavingMessage: false,
        evaId: id,
        evaStatus: true,
        showTabBar: false,
      })
      console.log(that.data.evaStatus);
    } else if (status == 'toAddEvaBtn') {
      // console.log("发表评论")  
      that.getComment();
    } else if (status == 'toNewsDetail') {
      // console.log("跳转至详情", id)
      let tmpData = that.data.newsList;
      let tmpUrl = '/longbing_card/pages/news/detail/detail?id=' + id
      if (tmpData[index].user_info.id) {
        console.log("1111111")
        tmpUrl = tmpUrl + '&isStaff=true'
      }
      wx.navigateTo({
        url: tmpUrl
      })
    }
    // console.log("官网 ===========================================")
    if (status == "toDetail") {
      // console.log("列表跳转至详情")
      let tmpData = that.data.companyData;
      // console.log("index", index, tmpData[index].name)
      if (tmpData[index].type == 5) {
        return false;
      } else {
        wx.navigateTo({
          url: '/longbing_card/pages/company/detail/detail?table_name=' + tmpData[index].table_name + '&type=' + tmpData[index].type + '&id=' + id + '&name=' + tmpData[index].name
        })
      }
    } else if (status == 'toCall') {
      if (!content || content == '尚未填写') {
        return false;
      }
      wx.makePhoneCall({
        phoneNumber: content,
        success: function (res) {
          // console.log('拨打电话成功 ==>>', res.data);
          if (app.globalData.to_uid != wx.getStorageSync("userid")) {
            that.toCopyRecord(type);
          }
        }
      });
    }
  },
  formSubmit: function (e) {
    var that = this;
    var formId = e.detail.formId;
    var status = e.detail.target.dataset.status;
    var index = e.detail.target.dataset.index;
    var type = e.detail.target.dataset.type;
    var text = e.detail.target.dataset.text;
    var content = e.detail.target.dataset.content;
    that.toSaveFormIds(formId);
    // console.log("********/////////////////////********************formId",formId)
    if (status == 'toTabBar') {
      wx.showShareMenu();
      that.setData({
        currentTabBar: type
      })
      if (type == 'toCard') {
        wx.setNavigationBarTitle({
          title: app.globalData.config.mini_app_name
        })
      } else {
        wx.setNavigationBarTitle({
          title: text
        })
      }
      wx.pageScrollTo({
        duration: 0,
        scrollTop: 0,
      });

      clearInterval(timerUnread);
      if (that.data.to_uid != wx.getStorageSync("userid")) {
        timerUnread = setInterval(function () {
          that.getClientUnread();
        }, 3000)
      }
    } else if (status == 'toCardMore') {
      if (content == 1) {
        // console.log("展开全部名片信息")
        that.setData({
          moreStatus: 2
        })
      } else if (content == 2) {
        // console.log("收起以下名片信息")
        that.setData({
          moreStatus: 1
        })
      }
    } else if (status == 'toCall') {
      // console.log("拨打电话")
      if (!content || content == '尚未填写') {
        return false;
      }
      wx.makePhoneCall({
        phoneNumber: content,
        success: function (res) {
          // console.log('拨打电话成功 ==>>', res.data);
          if (app.globalData.to_uid != wx.getStorageSync("userid")) {
            that.toCopyRecord(type);
          }
        }
      });
    } else if (status == 'toCopy') {
      // console.log("复制文本")
      if (!content || content == '尚未填写') {
        return false;
      }
      wx.setClipboardData({
        data: content,
        success: function (res) {
          wx.getClipboardData({
            success: function (res) {
              // console.log('复制文本成功 ==>>', res.data);
              if (app.globalData.to_uid != wx.getStorageSync("userid")) {
                that.toCopyRecord(type);
              }
            }
          });
        }
      });
    } else if (status == 'toMap') {
      // console.log("地图定位")
      if (!that.data.globalData.company.latitude || !that.data.globalData.company.longitude) {
        return false;
      }
      wx.openLocation({
        latitude: parseFloat(that.data.globalData.company.latitude),
        longitude: parseFloat(that.data.globalData.company.longitude),
        name: content,
        scale: 28,
        success: function (res) {
          // console.log("查看定位成功 ==> ", res)
          if (app.globalData.to_uid != wx.getStorageSync("userid")) {
            that.toCopyRecord(type);
          }
        }
      })
    } else if (status == 'toShowShare') {
      // console.log("显示 分_享名片")
      that.setData({
        showShareStatus: 1
      })
    } else if (status == 'toAddPhone') {
      // console.log("同步到通讯录 || 手机 座机 微信 邮箱 公司 地址")
      wx.addPhoneContact({
        firstName: that.data.cardIndexData.info.name,
        mobilePhoneNumber: that.data.cardIndexData.info.phone,
        hostNumber: that.data.cardIndexData.info.telephone,
        weChatNumber: that.data.cardIndexData.info.wechat,
        email: that.data.cardIndexData.info.email,
        organization: that.data.globalData.company.name,
        workAddressCity: that.data.globalData.company.addr,
        success: function (res) {
          // console.log("同步到通讯录成功 ==> ", res)
          if (app.globalData.to_uid != wx.getStorageSync("userid")) {
            that.toCopyRecord(type);
          }
        }
      })
    } else if (status == 'toShareCard') {
      // console.log("1微信好友 || 2名片码 || 3取消")
      if (type == 2) {
        wx.navigateTo({
          url: '/longbing_card/pages/card/share/share?to_uid=' + that.data.to_uid + '&form_id=' + that.data.from_id
        })
      }
      that.setData({
        showShareStatus: 0
      })
    } else if (status == "toNav") {
      let tmpData = that.data.companyData;
      // 1=>文章列表, 2=>图文详情, 3=>招聘信息, 4=>联系我们, 5=>员工展示
      if (tmpData[index].type == 2) {
        // console.log("图文详情")
        wx.navigateTo({
          url: '/longbing_card/pages/company/detail/detail?table_name=' + tmpData[index].table_name + '&type=' + tmpData[index].type + '&id=' + tmpData[index].info.id + '&name=' + tmpData[index].name
        })
      } else if (tmpData[index].type == 4) {
        // console.log("联系我们")
        wx.navigateTo({
          url: '/longbing_card/pages/company/contact/contact?identification=' + tmpData[index].identification + '&type=' + tmpData[index].type + '&name=' + tmpData[index].name
        })
      } else {
        // console.log("列表页面")
        wx.navigateTo({
          url: '/longbing_card/pages/company/list/list?identification=' + tmpData[index].identification + '&type=' + tmpData[index].type + '&name=' + tmpData[index].name + '&table_name=' + tmpData[index].table_name
        })
      }
    }

    if (that.data.currentTabBar == 'toCard') {
      // console.log("名片 =========================onShow==")
      that.setData({
        show: false,
        authStatus: 400,
      })
      // if (that.data.cardIndexData == '') {
        that.getCardIndexData();
      // }
      if (that.data.to_uid == wx.getStorageSync("userid")) {
        if (that.data.extensionsSelfList.length == 0) {
          that.ExtensionsSelf();
        }
      }
    } else if (that.data.currentTabBar == 'toShop') {
      // console.log("商城 =========================onShow==")  
      that.setData({
        show: false,
        authStatus: 400,
      })
      if (that.data.shopList.length == 0) {
        that.setData({
          pageShop: 1,
          moreShop: true,
          loading: false,
          isEmptyShop: false
        })
        that.getShopList();
      }
    } else if (that.data.currentTabBar == 'toNews') {
      // console.log("动态 =========================onShow==") 
      that.setData({
        show: false,
        authStatus: 400,
      })
      if (that.data.newsList.length == 0) {
        that.setData({
          pageNews: 1,
          moreNews: true,
          loading: false,
          isEmptyNews: false
        })
        that.getNewsList();
      }
    } else if (that.data.currentTabBar == 'toCompany') {
      // console.log("公司官网 =========================onShow==")
      that.setData({
        show: false,
        authStatus: 400,
      })
      if (that.data.companyData == '') {
        that.getCompanyData();
      }
    }
  },
  toSaveFormIds: function (formId) {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/formid',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': {
        formId: formId,
        user_id: wx.getStorageSync("userid")
      },
      success: function (res) {
        // console.log("entry/wxapp/formid ==>", res)
        if (!res.data.errno) {
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  }
})